//
//  DetailDynViewController.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/3/16.
//  Copyright © 2016年 zz. All rights reserved.
// 动态

#import "DetailDynViewController.h"
#import "DetailDynTableViewCell.h"
#import "DetailDynNoticeTableViewCell.h"
#import "DetailDynameTableViewCell.h"
@interface DetailDynViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *table;

@end

@implementation DetailDynViewController{
    
    UILabel *classLabel;
    UILabel *subLabel;
    UILabel *timeLabel;
    UILabel *nameLabel;
    SYAccount *accountDic;
}
@synthesize detailDic;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.nav_titleLabel.text = @"动态详情";
    classesArr=[NSArray array];
    [self createUI];
    if ([self.flag isEqualToString:@"tongzhi"]) {
        [self sysDetailData];
    }else
    {
//        NSDictionary * imformation = [Utils acquirePersonalImformationFromMemoryWithPersonID:USERID];
//        NSDictionary *userInfo = [imformation objectForKey:@"userinfo"];
        
        accountDic=[SYAccountTool ShareAccount].account;
        NSString *lasttype = [NSString stringWithFormat:@"%d",accountDic.lasttype];
        if ([lasttype isEqualToString:@"1"]) {
            [self detailData];
        }else
        {
             [self reloadStudentID];
        }
    }
    self.view.backgroundColor = [UIColor colorWithRed:0.9058 green:0.9316 blue:0.8812 alpha:1.0];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = backItem;
    
    self.automaticallyAdjustsScrollViewInsets=NO;

}

//查看历史动态
-(void)revewHistory{
    
    
}
-(void)sysDetailData
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_notice_get" forKey:@"method"];
    [dict setObject:_infoid forKey:@"noticeid"];
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
        NSDictionary * dict = (NSDictionary *)resposeJson;
        if ([[dict objectForKey:@"re_data"] count]>0) {
            self->detailDic =[[dict objectForKey:@"re_data"]objectAtIndex:0];
            [self->_table reloadData];
        }
        
        
    } failure:^(NSInteger recode, NSError * error){
        
    }];

}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createUI{
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(10,SafeAreaTopHeight, SCREENWIDTH - 20, [UIScreen mainScreen].bounds.size.height-SafeAreaTopHeight) style:UITableViewStylePlain];
    _table.showsVerticalScrollIndicator=NO;
    _table.delegate = self;
    _table.dataSource = self;
    _table.backgroundColor = [UIColor clearColor];
    _table.tableFooterView = [[UIView alloc ] init];
    [self.view addSubview:_table];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cell0=@"456";
    static NSString *cell1=@"123";
    NSString *className=[detailDic objectForKey:@"classname"];
    NSString *course = [detailDic objectForKey:@"coursename"];
    
    NSString * timeStr = [detailDic objectForKey:@"jointime"];
    timeStr = [Utils getDataStringWithoutSecondString:timeStr];
    NSString *time = timeStr;
    
    NSString *username = [detailDic objectForKey:@"username"];
    NSString *title=[detailDic objectForKey:@"title"];
    if (indexPath.section == 0) {
        
        if ([self.flag isEqualToString:@"tongzhi"]) {
            DetailDynNoticeTableViewCell *cell=[[DetailDynNoticeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell0];
            cell.titleLabel.text=[NSString stringWithFormat:@"标题： %@",title];
            cell.timeLabel.text=[NSString stringWithFormat:@"时间： %@",time];
             return cell;
        }
        else
        {
        DetailDynTableViewCell *cell = [[DetailDynTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell1];
        cell.classLabel.text = [NSString stringWithFormat:@"班级： %@",className];
        cell.subLabel.text = [NSString stringWithFormat:@"科目： %@",course];
        cell.timeLabel.text = [NSString stringWithFormat:@"时间： %@",time];
        cell.nameLabel.text = [NSString stringWithFormat:@"老师： %@",username];
       return cell;
        }
       
    }else{

        DetailDynameTableViewCell *cell=[[DetailDynameTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"789"];
        [cell upDateCellWithDic:detailDic];
        cell.layer.cornerRadius=10;
        return cell;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.flag isEqualToString:@"tongzhi"]) {
        if (indexPath.section==0) {
            return 95;
        }else
        {
            NSString * fileidsStr = [detailDic objectForKey:@"fileid"];
            
            NSInteger line = 0;
            
            
            NSArray *fileids;
            
            if (fileidsStr.length != 0) {
                
                fileids = [fileidsStr componentsSeparatedByString:@","];
            }
            
            
            if ([fileids count]%3 == 0) {
                line = [fileids count]/3;
            }else
            {
                line = [fileids count]/3 + 1;
            }
            
            return  (75 + 12.5) *line + [self resizeContenLabelHeightWithSting:[detailDic objectForKey:@"scontent"]] + 35;
        }
    }else
    {
        if (indexPath.section==0) {
            return 125;
        }else
        {
            NSString * fileidsStr = [detailDic objectForKey:@"fileid"];
            
            NSInteger line = 0;
            
            
            NSArray *fileids;
            
            if (fileidsStr.length != 0) {
                
                fileids = [fileidsStr componentsSeparatedByString:@","];
            }
            
            
            if ([fileids count]%3 == 0) {
                line = [fileids count]/3;
            }else
            {
                line = [fileids count]/3 + 1;
            }
            
            return  (75 + 12.5) *line + [self resizeContenLabelHeightWithSting:[detailDic objectForKey:@"scontent"]] + 35;
        }
    }
   
}
#pragma mark -- other
- (CGFloat)resizeContenLabelHeightWithSting:(NSString *)text
{
    if (text.length == 0) {
        return 0;
    }
    UIFont *font = [UIFont systemFontOfSize:12];
    
    NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin |
    NSStringDrawingUsesFontLeading;
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineBreakMode:NSLineBreakByCharWrapping];
    
    NSDictionary *attributes = @{ NSFontAttributeName : font, NSParagraphStyleAttributeName : style };
    
    CGSize maxSize = CGSizeMake(SCREENWIDTH - 15 - 55, CGFLOAT_MAX);
    CGSize size = [text boundingRectWithSize:maxSize
                                     options:opts
                                  attributes:attributes
                                     context:nil].size;
    return size.height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0;
    }else
    {
        return 10;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, _table.frame.size.width, 20);
    view.backgroundColor = [UIColor clearColor];
    return view;
}
-(void)reloadStudentID
{
    NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
    studentID=[ud objectForKey:@"studentID"];
    if (studentID!=nil) {
        [self detailData];
    }
   
}
#pragma mark -加载班级数
-(void)detailData
{
//   AppDelegate * appdelegate = CONSTAPPDELEGATE;
//    NSDictionary * imformation = [Utils acquirePersonalImformationFromMemoryWithPersonID:USERID];
//    NSDictionary *userInfo = [imformation objectForKey:@"userinfo"];
    NSString *lasttype = [NSString stringWithFormat:@"%d",accountDic.lasttype];
    NSMutableDictionary * requestDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    if ([lasttype isEqualToString:@"1"]) {
        [requestDic setObject:@"api_info_get" forKey:@"method"];
        [requestDic setObject:_infoid forKey:@"infoid"];
        [requestDic setObject:USERID forKey:@"userid"];
        [requestDic setObject:@"0" forKey:@"studentid"];
        if (self.classId!=nil) {
            [requestDic setObject:self.classId forKey:@"classid"];
            
        }
    }else
    {
        [requestDic setObject:@"api_info_getbystudent" forKey:@"method"];
        [requestDic setObject:_infoid forKey:@"infoid"];
        [requestDic setObject:USERID forKey:@"userid"];
        if (studentID!=nil) {
            [requestDic setObject:studentID forKey:@"studentid"];
        }
        if (self.classId!=nil) {
        [requestDic setObject:self.classId forKey:@"classid"];

        }
       
    }

    [Utils get:nil parameters:requestDic success:^(NSInteger recode, id resposeJson){
        NSDictionary *dic = (NSDictionary *)resposeJson;
        if ([[dic objectForKey:@"re_data"] count]>0) {
            
              [self->nodataImage removeFromSuperview];
              self->detailDic =[[dic objectForKey:@"re_data"]objectAtIndex:0];
              [self->_table reloadData];
        }else
        {
            
            self->nodataImage=[[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-95)/2, (self.view.frame.size.height-95)/2, 95, 95)];
            
            self->nodataImage.image=[UIImage imageNamed:@"nodata"];
            [self->_table addSubview:self->nodataImage];
        }
      
       
    } failure:^(NSInteger recode, NSError * error){
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
